﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class Admin_moredetails : System.Web.UI.Page
{
    DataSet DsetApplicantName = new DataSet();
    protected DataSet DsetAppliedJobs = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            populateTree();
            tdJobseekerDetails.Visible = false;
        }
    }
    private void populateTree()
    {
        JobSiteStarterKit.DAL.DBAccess obj = new JobSiteStarterKit.DAL.DBAccess();
        DsetApplicantName = obj.ExecuteDataSet("Proc_GetNameOfJobApplicants");
        if (DsetApplicantName.Tables.Count > 0)
        {
            if (DsetApplicantName.Tables[0].Rows.Count > 1)
            {
                for (int i = 0; i <= DsetApplicantName.Tables[0].Rows.Count - 1; i++)
                {
                    TreeNode node = new TreeNode();
                    node.Value = DsetApplicantName.Tables[0].Rows[i][0].ToString();
                    node.Text = DsetApplicantName.Tables[0].Rows[i][1].ToString();
                    TrvUserName.Nodes.Add(node);

                }
            }
            else
            {
                for (int i = 0; i <= DsetApplicantName.Tables[0].Rows.Count - 1; i++)
                {
                    TreeNode node = new TreeNode();
                    node.Value = DsetApplicantName.Tables[0].Rows[i][0].ToString();
                    node.Text = DsetApplicantName.Tables[0].Rows[i][1].ToString();
                    TrvUserName.Nodes.Add(node);

                }
            }
        }
        
    }
    protected void TrvUserName_SelectedNodeChanged(object sender, EventArgs e)
    {
        
        string jobseekerid = (string)TrvUserName.SelectedNode.Value;
        JobSiteStarterKit.DAL.DBAccess obj = new JobSiteStarterKit.DAL.DBAccess();
        System.Data.SqlClient.SqlParameterCollection objseeker = (System.Data.SqlClient.SqlParameterCollection)obj.Parameters; ;
        objseeker.Add("@JobSeekerId", SqlDbType.NVarChar, 10).Value = jobseekerid;
        DsetAppliedJobs = obj.ExecuteDataSet("Proc_AppliedJobsByUser");
        DGridAppliedJobs.DataSource = DsetAppliedJobs.Tables[0].DefaultView;
        DGridAppliedJobs.DataBind();
        tdJobseekerDetails.Visible = true;
        
    }
   
    protected void DGridAppliedJobs_RowDataBound1(object sender, GridViewRowEventArgs e)
    {
        Control ctrl = e.Row.FindControl("ChkAssign");
        if (ctrl != null)
        {
            CheckBox chk = (CheckBox)ctrl;

            if (DataBinder.Eval(e.Row.DataItem, "Status") != null)
            {
                string value = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Status"));
                if (value == "1")
                {
                    chk.Checked = true;
                }
            }
            else
            {
                chk.Checked = false;
            }
        }
    }
    protected void BtnAssign_Click(object sender, EventArgs e)
    {
        JobSiteStarterKit.DAL.DBAccess obj = new JobSiteStarterKit.DAL.DBAccess();
        obj.CommandText = "[Proc_AssignJobsToUser]";
        System.Data.SqlClient.SqlParameterCollection obj1 = (System.Data.SqlClient.SqlParameterCollection)obj.Parameters;

        string strid = MakeMyJobIdAsString();
        if (strid != "")
        {
            obj1.Add("@MyJobId", SqlDbType.NVarChar, 2000).Value = strid;
            obj.ExecuteNonQuery();
        }


    }
    public string MakeMyJobIdAsString()
    {
        string Id = "";
        string strid;
            for (int i = 0; i <= DGridAppliedJobs.Rows.Count - 1; i++)
            {
                Control ctrl = DGridAppliedJobs.Rows[i].FindControl("ChkAssign");
                CheckBox chk = (CheckBox)ctrl;

                if (chk.Checked == true)
                {
                    Id = Id + Convert.ToString(DGridAppliedJobs.DataKeys[i].Value);
                    Id = Id + "|";
                }
              
            }
            if (Id.Length >= 1)
            {
               strid = Id.Substring(0, Id.Length - 1);
                return strid;
            }
            strid = Id.Substring(0, Id.Length);
            return strid;
    }
}
